package loveqq.niceexcel.converter.internal.string;

import cn.hutool.core.util.StrUtil;
import loveqq.niceexcel.annotation.internal.Internal;
import loveqq.niceexcel.converter.CellData;
import loveqq.niceexcel.converter.ConvertContext;
import loveqq.niceexcel.converter.Converter;
import org.apache.poi.ss.usermodel.CellType;

/**
 * @author zhongjh@tsintergy.com
 * @date 2022-08-18
 */
@Internal
public class StringBooleanConverter implements Converter<String> {
    @Override
    public Class<String> supportJavaType() {
        return String.class;
    }

    @Override
    public CellType supportExcelType() {
        return CellType.BOOLEAN;
    }

    @Override
    public String convertToJavaData(CellData<?> cellData, ConvertContext context) {
        Boolean value = cellData.getBooleanValue();
        return (value == null) ? null : value.toString();
    }

    @Override
    public CellData<?> convertToExcelData(String data, ConvertContext context) {
        Boolean value = StrUtil.isEmpty(data) ? null : Boolean.valueOf(data);
        return new CellData<>(value);
    }

}
